package com.ouc.openplatform.pampas;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class InnerCookie implements Serializable
{
  private static final long serialVersionUID = -5440120009211709421L;
  
  public Set<FakeCookie> getNewCookies()
  {
    return this.newCookies;
  }
  
  private final Set<FakeCookie> newCookies = new HashSet();
  
  public Set<FakeCookie> getDelCookies()
  {
    return this.delCookies;
  }
  
  private final Set<FakeCookie> delCookies = new HashSet();
  private final Map<String, String> cookies;
  
  public InnerCookie(Map<String, String> cookies)
  {
    this.cookies = cookies;
  }
  
  public String get(String name)
  {
    return (String)this.cookies.get(name);
  }
  
  public void set(String name, String value)
  {
    set(name, value, -1);
  }
  
  public void set(String name, String value, String domain)
  {
    set(name, value, domain, -1);
  }
  
  public void set(String name, String value, String domain, int age)
  {
    FakeCookie fakeCookie = new FakeCookie();
    fakeCookie.name = name;
    fakeCookie.value = value;
    fakeCookie.domain = domain;
    fakeCookie.age = age;
    set(fakeCookie);
  }
  
  public void set(String name, String value, int age)
  {
    set(name, value, age, "/");
  }
  
  public void set(String name, String value, int age, String path)
  {
    FakeCookie fakeCookie = new FakeCookie();
    fakeCookie.name = name;
    fakeCookie.value = value;
    fakeCookie.age = age;
    fakeCookie.path = path;
    set(fakeCookie);
  }
  
  public void set(FakeCookie fakeCookie)
  {
    if (fakeCookie.name == null) {
      throw new IllegalArgumentException("cookie name should not be null");
    }
    this.newCookies.add(fakeCookie);
  }
  
  public void remove(String name)
  {
    remove(name, "/");
  }
  
  public void remove(String name, String path)
  {
    remove(name, path, "");
  }
  
  public void removeInDomain(String name, String domain)
  {
    remove(name, "/", domain);
  }
  
  public void remove(String name, String path, String domain)
  {
    FakeCookie fakeCookie = new FakeCookie();
    fakeCookie.name = name;
    fakeCookie.path = path;
    fakeCookie.domain = domain;
    this.delCookies.add(fakeCookie);
  }
  
  public void merge(InnerCookie innerCookie)
  {
    this.newCookies.addAll(innerCookie.newCookies);
    this.delCookies.addAll(innerCookie.delCookies);
  }
  
  public static class FakeCookie
    implements Serializable
  {
    private static final long serialVersionUID = 2337241241780428991L;
    private String name;
    private String value;
    
    public String getName()
    {
      return this.name;
    }
    
    public void setName(String name)
    {
      this.name = name;
    }
    
    public String getValue()
    {
      return this.value;
    }
    
    public void setValue(String value)
    {
      this.value = value;
    }
    
    public String getDomain()
    {
      return this.domain;
    }
    
    private String domain = "";
    
    public void setDomain(String domain)
    {
      this.domain = domain;
    }
    
    public String getPath()
    {
      return this.path;
    }
    
    private String path = "/";
    
    public void setPath(String path)
    {
      this.path = path;
    }
    
    public int getAge()
    {
      return this.age;
    }
    
    private int age = -1;
    
    public void setAge(int age)
    {
      this.age = age;
    }
    
    public boolean isHttpOnly()
    {
      return this.httpOnly;
    }
    
    private boolean httpOnly = false;
    
    public void setHttpOnly(boolean httpOnly)
    {
      this.httpOnly = httpOnly;
    }
    
    public boolean equals(Object o)
    {
      if (this == o) {
        return true;
      }
      if (!(o instanceof FakeCookie)) {
        return false;
      }
      FakeCookie that = (FakeCookie)o;
      if (this.domain != null ? !this.domain.equals(that.domain) : that.domain != null) {
        return false;
      }
      if (!this.name.equals(that.name)) {
        return false;
      }
      if (!this.path.equals(that.path)) {
        return false;
      }
      return true;
    }
    
    public int hashCode()
    {
      int result = this.name.hashCode();
      result = 31 * result + (this.domain != null ? this.domain.hashCode() : 0);
      result = 31 * result + this.path.hashCode();
      return result;
    }
  }
}
